package com.codeoss.framework.webide.app.service.impl;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.context.YunContext;
import com.codeoss.framework.data.metadata.Query;
import com.codeoss.framework.data.metadata.Restrictions;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.impexp.ExportManager;
import com.codeoss.framework.impexp.ImportManager;
import com.codeoss.framework.webide.app.service.CommonAppDataHandler;
import com.codeoss.module.ir.model.IrApplication;
import com.codeoss.module.ir.model.IrDict;
import com.codeoss.module.ir.model.IrDictLine;
import com.codeoss.module.ir.service.IIrDictLineService;
import com.codeoss.module.ir.service.IIrDictService;

@Service("ir_dict_lineAppDataHandler")
public class IrDictLineAppDataHandler extends CommonAppDataHandler {
    @Autowired
    private IIrDictLineService irDictLineService;
    @Autowired
    private IIrDictService irDictService;
    @Autowired
    private ExportManager exportManager;
    @Autowired
    private ITableStore tableStore;
    @Autowired
  	private ImportManager importManager;
	@Override
	public void dataExport(YunContext yunContext, IrApplication irApplication, String tableName) throws IOException{
		// TODO Auto-generated method stub
		QueryWrapper<IrDict> queryWrapper=new QueryWrapper<IrDict>();
		queryWrapper.in("module_type", getModuleTypeList(yunContext, irApplication));
		queryWrapper.orderByAsc("id");
		List<IrDict> irDictList=irDictService.list(queryWrapper);
		int index=0;
		for (int i = 0; i < irDictList.size(); i++) {
			IrDict irDict=irDictList.get(i);
			List<IrDictLine> lineList=irDictLineService.getDictLineListByDictId(irDict.getId());
			for (int j = 0; j < lineList.size(); j++) {
				IrDictLine irDictLine=lineList.get(j);
				Table table=tableStore.get("ir_dict_line");
				Query query=new Query();
				query.add(Restrictions.eq("id", irDictLine.getId()));
				String filename="ir_dict_line"+index;
				writeConfigKeyValue(yunContext, "ir_dict_line", filename);
				FileOutputStream os=createFileOutputStream(yunContext, filename);
				exportManager.exportProperties(yunContext, table, query, os);
				os.close();
				index++;
			}
		}	
	}

	@Override
	@Transactional
	public void dataImport(YunContext yunContext, IrApplication irApplication, String tableName, List<String> filenameList)throws IOException {
		// TODO Auto-generated method stub
		if (filenameList == null) {
			return;
		}
		for(int i=0;i<filenameList.size();i++) {
			String filename=filenameList.get(i);
			InputStream is=createFileInputStream(yunContext, filename);
			Table table = tableStore.get("ir_dict_line");
			importManager.importProperties(yunContext, table, is);
			is.close();
		}
	}

}
